package com.xw.bo.ks.entity.base;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 主要是围绕QueryWrapper对象服务
 *
 * @author： XW -> ~
 * @create： 2023/12/8 9:52
 */
@Slf4j
@Data
public class CollectionBuild {

    private CollectionBuild collectionBuild;
    private Map<String, Object> wrapper;
    private Map<String, List<Integer>> inIntWrapper;
    private Map<String, List<Long>> inLongWrapper;
    private Map<String, List<String>> inStringWrapper;
    private List<String> list;

    private CollectionBuild(){

    }

    public static CollectionBuild create() {
        return create(null);
    }

    public static CollectionBuild create(Integer mapType) {
        CollectionBuild build = new CollectionBuild();
        if (mapType == null || mapType == 1) {
            build.wrapper = new HashMap<>();
            build.inIntWrapper = new HashMap<>();
            build.inLongWrapper = new HashMap<>();
            build.inStringWrapper = new HashMap<>();
        } else {
            build.wrapper = new ConcurrentHashMap<>();
            build.inIntWrapper = new ConcurrentHashMap<>();
            build.inLongWrapper = new ConcurrentHashMap<>();
            build.inStringWrapper = new ConcurrentHashMap<>();
        }
        build.list = new ArrayList<>();

        return build;
    }

    public CollectionBuild put(String key, Object value) {
        if (null == value) {
            return this;
        }
        if (value instanceof String) {
            if (StringUtils.isEmpty(value.toString())){
                return this;
            }
        }

        wrapper.put(key, value);
        return this;
    }

    public CollectionBuild putIncludeNull(String key, Object value) {
        wrapper.put(key, value);
        return this;
    }

    public CollectionBuild putIntList(String key, List<Integer> valueList) {
        if (CollectionUtils.isEmpty(valueList)){
            return this;
        }
        inIntWrapper.put(key, valueList);
        return this;
    }

    public CollectionBuild putStringList(String key, List<String> valueList) {
        inStringWrapper.put(key, valueList);
        return this;
    }

    public CollectionBuild putLongList(String key, List<Long> valueList) {
        inLongWrapper.put(key, valueList);
        return this;
    }

    public CollectionBuild add(String value) {
        list.add(value);
        return this;
    }

    public Map<String, Object> buildMap() {
        return wrapper;
    }

    public Map<String, List<Integer>> buildIntMap() {
        return inIntWrapper;
    }

    public Map<String, List<Long>> buildLongMap() {
        return inLongWrapper;
    }

    public Map<String, List<String>> buildStringMap() {
        return inStringWrapper;
    }

    public List<String> buildList() {
        return list;
    }

    public static void main(String[] args) {

        Map<String, Map<String, List>> inWrapper = new HashMap<>();
        Map<String, List> orDefault = inWrapper.getOrDefault(("Object"), new HashMap<String, List>());
        orDefault.put("aa", new ArrayList());
        inWrapper.put("Object", orDefault);
        log.info("objectList = {}", JSON.toJSONString(inWrapper));

    }

}
